package com.yylending.las.lending.quota.baseinfo;

import android.content.Context;
import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.common.AESEntity;
import com.yylending.las.common.location.entity.Location;
import com.yylending.las.common.utils.area.AddressCityBean;
import com.yylending.las.common.utils.area.AddressDistrictBean;
import com.yylending.las.common.utils.area.AddressProvinceBean;
import com.yylending.las.lending.quota.entity.PersonalBean;
import com.yylending.las.lending.quota.initdata.InitDataModel;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by laixj on 2017/6/19.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class BaseInfoPresenter extends Presenter<BaseInfoModel, BaseInfoPresenter.View> {
	private final static String TAG = "BaseInfoPresenter";

	public interface View extends IView {
		/**
		 * 保存基本信息或开始申请额度或获取人脸识别状态或获取基本信息返回
		 */
		void saveBaseInfoSuccess();

		void saveBaseInfoFail(String msg);

		/*void getBaseInfoSuccess(PersonalBean info);

		void getBaseInfoFail(String msg);*/

		/**
		 * 初始化常住地区
		 */
		void initAreaSuccess(List<BaseInfoModel.AccessCity> areas);

		void initAreaFail(String msg);
	}

	public static class Generator implements PresenterFactory<BaseInfoPresenter> {
		@Override
		public BaseInfoPresenter create() {
			return new BaseInfoPresenter();
		}
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new BaseInfoModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/**
	 * 保存基本信息
	 */
	public void saveBaseInfo(PersonalBean baseInfo) {
		execute(new AESEntity(baseInfo), BaseInfoModel.METHOD_SAVE_BASE_INFO, true, baseInfo, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void saveBaseInfoResult(BaseInfoModel.SaveInfoResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				CLog.i(TAG, "rs=" + result.toString());
				saveBaseInfoLocal((PersonalBean) result.extraParam);
				mView.saveBaseInfoSuccess();
			} else {
				String msg = result.getRetMsg();
				mView.saveBaseInfoFail(TextUtils.isEmpty(msg) ? "保存基本信息失败" : msg);
			}
		}
	}

	/**
	 * 获取基本信息
	 */
	/*public void getBaseInfo() {
		execute(new AESEntity(new BaseInfoModel.InfoType(Constants.CUSTOMER_TYPE_BASEINFO)), BaseInfoModel.METHOD_GET_BASE_INFO, false, Constants.CUSTOMER_TYPE_BASEINFO, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getBaseInfoResult(BaseInfoModel.BaseInfoResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			String type = (String) result.extraParam;
			if (BaseResultEntity.isResponseSuccess(result) && null != result.getData() && !TextUtils.isEmpty(result.getData().json)) {
				BaseInfoModel.AesData data = result.getData();
				CLog.i(TAG, "服务器基本信息encode=" + data.json);
				if (TextUtils.isEmpty(Global.KEY_AES)) {
					mView.getBaseInfoFail("基本信息不存在");
					return;
				}
				CLog.i(TAG, "服务器基本信息decode=" + Cryptos.aesDecrypt(data.json, Global.KEY_AES));
				CustomerBean customerBean = new Gson().fromJson(Cryptos.aesDecrypt(data.json, Global.KEY_AES), CustomerBean.class);
				if (Constants.CUSTOMER_TYPE_BASEINFO.equals(type)) {
					if (null == customerBean || null == customerBean.baseInfo || TextUtils.isEmpty(customerBean.baseInfo.addP) || TextUtils.isEmpty(customerBean.baseInfo.addC)) {
						mView.getBaseInfoFail("基本信息不存在");
					} else {
						PersonalBean baseInfo = customerBean.baseInfo;
						CLog.i(TAG, "服务器基本信息decode=" + Cryptos.aesDecrypt(data.json, Global.KEY_AES));
						CLog.i(TAG, "getBaseInfo rs=" + baseInfo.toString());
						saveBaseInfoLocal(baseInfo);
						mView.getBaseInfoSuccess(baseInfo);
					}
				}
			} else {
				String msg = result.getRetMsg();
				mView.getBaseInfoFail(TextUtils.isEmpty(msg) ? "基本信息不存在" : msg);
			}
		}
	}*/

	/**
	 * 获取常住地址(开放地区)
	 */
	public void getAreaList() {
		execute(null, BaseInfoModel.METHOD_GET_SUPPORT_AREA, false, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getAreaResult(BaseInfoModel.AreaResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result) && null != result.getData() && null != result.getData().cities) {
				List<BaseInfoModel.AccessCity> rs = result.getData().cities;
				CLog.i(TAG, "mAreaData=" + rs.toString());
				mView.initAreaSuccess(result.getData().cities);
			} else {
				mView.initAreaFail(null);
			}
		}
	}

	private static void saveBaseInfoLocal(PersonalBean baseInfo) {
		BaseInfoModel.saveBaseInfoLocal(baseInfo);
	}

	/**
	 * 获取选项卡信息
	 */
	public static InitDataModel.Options getOptionList() {
		return BaseInfoModel.getOptionList();
	}

	/**
	 * 判断是否是开放城市
	 */
	public static BaseInfoModel.OpenCity judgeCity(ArrayList<AddressProvinceBean> provinceList, ArrayList<ArrayList<AddressCityBean>> cityList,
			ArrayList<ArrayList<ArrayList<AddressDistrictBean>>> countyList, Location location) {
		if (null == provinceList || null == cityList || null == countyList || null == location || null == location.address) {
			return null;
		}
		return BaseInfoModel.judgeCity(provinceList, cityList, countyList, location.address.province, location.address.city, location.address.district, "areaName");
	}

	/**
	 * 判断是否是开放城市
	 */
	public static BaseInfoModel.OpenCity judgeCity(ArrayList<AddressProvinceBean> provinceList, ArrayList<ArrayList<AddressCityBean>> cityList,
			ArrayList<ArrayList<ArrayList<AddressDistrictBean>>> countyList, String province, String city, String county, String type) {
		return BaseInfoModel.judgeCity(provinceList, cityList, countyList, province, city, county, type);
	}

	public void getAccessAddress(Context context, List<BaseInfoModel.AccessCity> areaList, ArrayList<AddressProvinceBean> mProvince,
			ArrayList<ArrayList<AddressCityBean>> mCity, ArrayList<ArrayList<ArrayList<AddressDistrictBean>>> mDistrict) {
		mModel.getAccessAddress(context, areaList, mProvince, mCity, mDistrict);
	}
}
